import load from '@/utils/jsapi.js';
import JSON from 'json3';
import mapsenceViewPopup from '@/utils/maputils/mapsenceViewPopup.js';
import mapconfig from '@/config/config_map.js'
import store from '@/store/index'
import {
  Message
} from 'element-ui';
const mapUIoperation = {

  async toggleMapUI(Parameters) {
    const [
      BMapGallery_Expand,
      DistanceMeasureMentButton3D,
      AreaMeasureMentButton3D,
      MapSceneSwitch_Control,
      LayerList,
      Expand,
      Home,
      Fullscreen,
      Zoom,
      HoloGraphicButton,
      AddressLocationSearch,
      GridButton,
      BaseMapSwitchButton3D,
      AboveBelowModeSwitch
    ] = await load([
      "esri/widgets/BMapGallery_Expand",
      "esri/widgets/DistanceMeasureMentButton3D",
      "esri/widgets/AreaMeasureMentButton3D",
      "esri/widgets/MapSceneSwitch_Control",
      "esri/widgets/LayerList",
      "esri/widgets/Expand",
      "esri/widgets/Home",
      "esri/widgets/Fullscreen",
      "esri/widgets/Zoom",
      "esri/widgets/HoloGraphicButton",
      "esri/widgets/AddressLocationSearch",
      "esri/widgets/GridButton",
      "esri/widgets/BaseMapSwitchButton3D",
      "esri/widgets/AboveBelowModeSwitch",
    ]);
    Parameters.UIName.map(function (item, key, ary) {
      if (Parameters.Action.toUpperCase() === "ADD") {
        switch (item.toUpperCase()) {
          case 'BMAPGALLERYEXPAND'://BMapGalleryExpand
            if (window.BMapGallery_Expand) {} else {
              window.BMapGallery_Expand = new BMapGallery_Expand({
                container: document.createElement("div"),
                view: window.sceneView,
                showBuildingBlock:false
              });
            }
            if (Parameters.UIlocation === null || Parameters.UIlocation === undefined || Parameters.UIlocation === '{}') {
              window.sceneView.ui.add(window.BMapGallery_Expand, "top-right");
            } else {
              window.sceneView.ui.add(window.BMapGallery_Expand, Parameters.UIlocation);
            }
            break;
          case 'LOCATIONSEARCH': //LocationSearch
            if (window.AddressLocationSearchExpand && window.AddressLocationSearch) {} else {
              window.AddressLocationSearch = new AddressLocationSearch({
                view: window.sceneView,
                locationaddress: mapconfig.locationaddress,
                mapservicetoken: store.state.setting.maptoken
              })
              window.AddressLocationSearchExpand = new Expand({
                // id: "addresslocationsearch",
                content: window.AddressLocationSearch,
                expandIconClass: "esri-icon-search",
                expandTooltip: "地名地址搜索",
                expanded: true,
                group: "top-left"
              });
            }
            if (Parameters.UIlocation === null || Parameters.UIlocation === undefined || Parameters.UIlocation === '{}') {
              window.sceneView.ui.add(window.AddressLocationSearchExpand, "top-right");
            } else {
              window.sceneView.ui.add(window.AddressLocationSearchExpand, Parameters.UIlocation);
            }
            break;
          case 'DISTANCEMEASUREMENTBUTTON3D': //DistanceMeasureMentButton3D
            if (window.DistanceMeasureMentButton3D) {} else {
              window.DistanceMeasureMentButton3D = new DistanceMeasureMentButton3D({
                view: window.sceneView,
              });
            }
            if (Parameters.UIlocation === null || Parameters.UIlocation === undefined || Parameters.UIlocation === '{}') {
              window.sceneView.ui.add(window.DistanceMeasureMentButton3D, "top-right");
            } else {
              window.sceneView.ui.add(window.DistanceMeasureMentButton3D, Parameters.UIlocation);
            }
            break;
          case 'AREAMEASUREMENTBUTTON3D': //AreaMeasureMentButton3D
            if (window.AreaMeasureMentButton3D) { } else {
              window.AreaMeasureMentButton3D = new AreaMeasureMentButton3D({
                view: window.sceneView,
              });
            }
            if (Parameters.UIlocation === null || Parameters.UIlocation === undefined || Parameters.UIlocation === '{}') {
              window.sceneView.ui.add(window.AreaMeasureMentButton3D, "top-right");
            } else {
              window.sceneView.ui.add(window.AreaMeasureMentButton3D, Parameters.UIlocation);
            }
            break;
          case 'LAYERLIST':
            if (window.LayerListExpand && window.LayerList) {} else {
              window.LayerList = new LayerList({
                container: document.createElement("div"),
                id: "maplayerlist",
                view: window.sceneView,
              });
              window.LayerListExpand = new Expand({
                view: window.sceneView,
                content: window.LayerList.domNode,
                expandIconClass: "esri-icon-layers",
                expandTooltip: "图层框",
                group: "top-right"
              });
            }
            if (Parameters.UIlocation === null || Parameters.UIlocation === undefined || Parameters.UIlocation === '{}') {
              window.sceneView.ui.add(window.LayerListExpand, "top-right");
            } else {
              window.sceneView.ui.add(window.LayerListExpand, Parameters.UIlocation);
            }
            break;
          case 'MAPSCENESWITCH':
            if (window.MapSceneSwitch != null) {} else {
              window.MapSceneSwitch = new MapSceneSwitch_Control({
                container: document.createElement("div"),
                view: window.sceneView,
                camera: window.camera
              });
            }
            if (Parameters.UIlocation === null || Parameters.UIlocation === undefined || Parameters.UIlocation === '{}') {
              window.sceneView.ui.add(window.MapSceneSwitch, "top-right");
            } else {
              window.sceneView.ui.add(window.MapSceneSwitch, Parameters.UIlocation);
            }
            break;
          case 'HOME':
            if (window.homeWidget != null) {} else {
              window.homeWidget = new Home({
                view: window.sceneView
              });
            }
            if (Parameters.UIlocation === null || Parameters.UIlocation === undefined || Parameters.UIlocation === '{}') {
              window.sceneView.ui.add(window.homeWidget, "top-right");
            } else {
              window.sceneView.ui.add(window.homeWidget, Parameters.UIlocation);
            }
            break;
          case 'FULLSCREEN':
            if (window.fullscreen != null) {} else {
              window.fullscreen = new Fullscreen({
                container: document.createElement("div"),
                view: window.sceneView
              });
            }
            if (Parameters.UIlocation === null || Parameters.UIlocation === undefined || Parameters.UIlocation === '{}') {
              window.sceneView.ui.add(window.fullscreen, "top-right");
            } else {
              window.sceneView.ui.add(window.fullscreen, Parameters.UIlocation);
            }
            break;
          case 'ZOOM':
            if (window.Zoom != null) {} else {
              window.Zoom = new Zoom({
                container: document.createElement("div"),
                view: window.sceneView
              });
            }
            if (Parameters.UIlocation === null || Parameters.UIlocation === undefined || Parameters.UIlocation === '{}') {
              window.sceneView.ui.add(window.Zoom, "top-right");
            } else {
              window.sceneView.ui.add(window.Zoom, Parameters.UIlocation);
            }
            break;
          case 'HOLOGRAPHICBUTTON':
            if (window.HoloGraphicButton != null) {} else {
              window.HoloGraphicButton = new HoloGraphicButton({
                container: document.createElement("div"),
                view: window.sceneView,
              });
            }
            if (Parameters.UIlocation === null || Parameters.UIlocation === undefined || Parameters.UIlocation === '{}') {
              window.sceneView.ui.add(window.HoloGraphicButton, "top-right");
            } else {
              window.sceneView.ui.add(window.HoloGraphicButton, Parameters.UIlocation);
            }
            break;
          case 'GRID':
            if (window.GridButton != null) {} else {
              window.GridButton = new GridButton({
                container: document.createElement("div"),
                view: window.sceneView,
              });
            }
            if (Parameters.UIlocation === null || Parameters.UIlocation === undefined || Parameters.UIlocation === '{}') {
              window.sceneView.ui.add(window.GridButton, "top-right");
            } else {
              window.sceneView.ui.add(window.GridButton, Parameters.UIlocation);
            }
            break;
          case 'BASEMAPSWITCHBUTTON3D': //BaseMapSwitchButton3D
            if (window.BaseMapSwitchButton != null) {} else {
              window.BaseMapSwitchButton3D = new BaseMapSwitchButton3D({
                view: window.sceneView,
                showBuildingBlock:false
              });
            }
            if (Parameters.UIlocation === null || Parameters.UIlocation === undefined || Parameters.UIlocation === '{}') {
              window.sceneView.ui.add(window.BaseMapSwitchButton3D, "top-right");
            } else {
              window.sceneView.ui.add(window.BaseMapSwitchButton3D, Parameters.UIlocation);
            }
            break;
          case 'ABOVEBELOWMODESWITCH':
            if (window.AboveBelowModeSwitch != null) {} else {
              window.AboveBelowModeSwitch = new AboveBelowModeSwitch({
                container: document.createElement("div"),
                view: window.sceneView,
              });
            }
            if (Parameters.UIlocation === null || Parameters.UIlocation === undefined || Parameters.UIlocation === '{}') {
              window.sceneView.ui.add(window.AboveBelowModeSwitch, "top-right");
            } else {
              window.sceneView.ui.add(window.AboveBelowModeSwitch, Parameters.UIlocation);
            }
            break;
          default:
            Message({
              message: "当前" + item + "组件不存在",
              type: 'warning',
              duration: 2 * 1000
            })
        }
      } else {
        switch (item.toUpperCase()) {
          case 'BMAPGALLERYEXPAND':
            if (window.BMapGallery_Expand != null)
              window.sceneView.ui.remove(window.BMapGallery_Expand);
            break;
          case 'LOCATIONSEARCH': //LocationSearch
            if (window.AddressLocationSearchExpand != null)
              window.sceneView.ui.remove(window.AddressLocationSearchExpand);
            break;
          case 'DISTANCEMEASUREMENTBUTTON3D':
            if (window.DistanceMeasureMentButton3D != null)
              window.sceneView.ui.remove(window.DistanceMeasureMentButton3D);
            break;
          case 'AREAMEASUREMENTBUTTON3D':
            if (window.AreaMeasureMentButton3D != null)
              window.sceneView.ui.remove(window.AreaMeasureMentButton3D);
            break;
            
          case 'LAYERLIST':
            if (window.LayerListExpand != null)
              window.sceneView.ui.remove(window.LayerListExpand);
            break;
          case 'MAPSCENESWITCH':
            if (window.MapSceneSwitch != null)
              window.sceneView.ui.remove(window.MapSceneSwitch);
            break;

          case 'HOME':
            if (window.homeWidget != null)
              window.sceneView.ui.remove(window.homeWidget);
            break;

          case 'FULLSCREEN':
            if (window.fullscreen != null)
              window.sceneView.ui.remove(window.fullscreen);
            break;
          case 'ZOOM':
            if (window.Zoom != null)
              window.sceneView.ui.remove(window.Zoom);
            break;
          case 'HOLOGRAPHICBUTTON':
            if (window.HoloGraphicButton != null)
              window.sceneView.ui.remove(window.HoloGraphicButton);
            break;
          case 'GRID':
            if (window.GridButton != null)
              window.sceneView.ui.remove(window.GridButton);
            break;
          case 'BASEMAPSWITCHBUTTON3D':
            if (window.BaseMapSwitchButton3D != null)
              window.sceneView.ui.remove(window.BaseMapSwitchButton3D);
            break;
          case 'ABOVEBELOWMODESWITCH':
            if (window.AboveBelowModeSwitch != null)
              window.sceneView.ui.remove(window.AboveBelowModeSwitch);
            break;
          default:
            break;
        }
      }
    })
  },

  setmapuipadding(Parameters) {
    if (Parameters.uiPadding != null && Parameters.uiPadding != undefined)
      window.sceneView.ui.padding = Parameters.uiPadding;
  },
  Togglepopuptemplate(Parameters) {
    if (Parameters.isvisible === true) {
      window.sceneView.popup.autoOpenEnabled = true;
    } else {
      window.sceneView.popup.autoOpenEnabled = false;
    }

  },
  async setpopupcontent(Parameters) {
    const [
      Point,
      SpatialReference
    ] = await load([
      "esri/geometry/Point",
      "esri/geometry/SpatialReference"
    ]);

    const dockpoint = new Point({
      x: Parameters.location.codX,
      y: Parameters.location.codY,
      z: Parameters.location.codZ,
      spatialReference: SpatialReference.WebMercator
    });
    window.sceneView.popup.open({
      actions: [],
      location: dockpoint,
      title: Parameters.title,
      collapseEnabled: false,
      dockEnabled: false,
      content: mapsenceViewPopup.createCustompopup(Parameters.content)
    });
  }

};
export default mapUIoperation
